package net.jlibrary.common.project.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.BatchSize;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;

import net.jlibrary.common.core.annotation.CodeGenComment;
import net.jlibrary.common.core.annotation.Compositor;
import net.jlibrary.common.core.annotation.SearchCondition;
import net.jlibrary.common.core.annotation.SearchField;
import net.jlibrary.common.core.entity.BaseObject;
import net.jlibrary.common.core.entity.IValid;

@Entity(name = "tab_dictionary")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "category", discriminatorType = DiscriminatorType.STRING, length = 30)
@BatchSize(size = 15)
@Compositor("taxis asc,createdate desc")
public class Dictionary extends BaseObject implements IValid {
	private static final long serialVersionUID = -5181952659949488138L;
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@SearchField(value = "id", condition = SearchCondition.EQUAL)
	protected Long id;
	@Column(length = 50)
	@SearchField(value = "name", condition = SearchCondition.EQUAL)
	@CodeGenComment(value = "类别名称", viewList = true, viewSearch = true, viewText = true, required = true)
	protected String name;// 名称
	@Column(length = 50)
	@SearchField(value = "code", condition = SearchCondition.EQUAL)
	@CodeGenComment(value = "类别编码", viewList = true, viewSearch = true, required = true)
	protected String code;// 代码
	@Column(length = 200)
	@CodeGenComment(value = "类别描述", viewList = true)
	protected String description;// 描述
	@CodeGenComment(value = "排序", viewList = true)
	protected int taxis;// 排序
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "parent_id")
	@SearchField(value = "parent", condition = SearchCondition.EQUAL)
	@CodeGenComment(value = "上级类别", viewList = true, viewSearch = true)
	@JsonBackReference
	protected Dictionary parent;
	@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
	@OrderBy("taxis asc")
	@JsonManagedReference
	protected Set<Dictionary> childs = new HashSet<Dictionary>();
	@CodeGenComment(value = "有效性", viewList = true, viewSearch = true)
	protected boolean valid;
	@Column(updatable = false)
	@Temporal(TemporalType.TIMESTAMP)
	@CodeGenComment(value = "创建时间", viewList = true)
	protected Date createdate;
	@Temporal(TemporalType.TIMESTAMP)
	@CodeGenComment(value = "最后修改时间")
	protected Date lastdate;

	@Transient
	protected boolean allowQueryInvalid;
	@Transient
	protected boolean compellingDelete;
	@Transient
	@SearchField(value = "parent", condition = SearchCondition.IS_NULL)
	protected String parentIsNull;
	@Transient
	@SearchField(value = "valid", condition = SearchCondition.EQUAL, type = "Boolean")
	private String validString;

	public Long getId() {
		return id;
	}

	@Override
	public Serializable getPrimaryKey() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public int getTaxis() {
		return taxis;
	}

	public void setTaxis(int taxis) {
		this.taxis = taxis;
	}

	public Dictionary getParent() {
		return parent;
	}

	public void setParent(Dictionary parent) {
		this.parent = parent;
	}

	public Set<Dictionary> getChilds() {
		return childs;
	}

	public void setChilds(Set<Dictionary> childs) {
		this.childs = childs;
	}

	public boolean isValid() {
		return valid;
	}

	public void setValid(boolean valid) {
		this.valid = valid;
	}

	public Date getCreatedate() {
		return createdate;
	}

	public void setCreatedate(Date createdate) {
		this.createdate = createdate;
	}

	public Date getLastdate() {
		return lastdate;
	}

	public void setLastdate(Date lastdate) {
		this.lastdate = lastdate;
	}

	public boolean isAllowQueryInvalid() {
		return allowQueryInvalid;
	}

	public void setAllowQueryInvalid(boolean allowQueryInvalid) {
		this.allowQueryInvalid = allowQueryInvalid;
	}

	public boolean isCompellingDelete() {
		return compellingDelete;
	}

	public void setCompellingDelete(boolean compellingDelete) {
		this.compellingDelete = compellingDelete;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Dictionary other = (Dictionary) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	public String getParentIsNull() {
		return parentIsNull;
	}

	public void setParentIsNull(String parentIsNull) {
		this.parentIsNull = parentIsNull;
	}

	public String getValidString() {
		return validString;
	}

	public void setValidString(String validString) {
		this.validString = validString;
	}
}
